<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <!-- <el-form-item label="产品id" prop="goodsId">
                <el-input v-model="queryParams.goodsId" placeholder="请输入产品id"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item> -->
            <el-form-item label="生产单号" prop="produceNo">
              <el-input v-model="queryParams.produceNo" placeholder="请输入生产单号"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="订单编号" prop="projectNo">
              <el-input v-model="queryParams.projectNo" placeholder="请输入订单编号"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="物料编码" prop="goodsCode">
              <el-input v-model="queryParams.goodsCode" placeholder="请输入物料编码"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="物料名称" prop="goodsName">
                <el-input v-model="queryParams.goodsName" placeholder="请输入物料名称"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <!--<el-form-item label="物料型号明细" prop="goodsBrand">
                <el-input v-model="queryParams.goodsBrand" placeholder="请输入物料型号明细"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>-->
            <el-form-item label="物料型号" prop="goodModel">
                <el-input v-model="queryParams.goodModel" placeholder="请输入物料型号"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
           <!-- <el-form-item label="物料单价" prop="goodsBuyPrice">
                <el-input v-model="queryParams.goodsBuyPrice" placeholder="请输入物料单价"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="物料本价" prop="goodsBuyTotal">
                <el-input v-model="queryParams.goodsBuyTotal" placeholder="请输入物料本价"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="供应商" prop="supplierId">
                <el-input v-model="queryParams.supplierId" placeholder="请输入供应商"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>-->
           <!-- <el-form-item label="销售人姓名" prop="saleName">
                <el-input v-model="queryParams.saleName" placeholder="请输入销售人姓名"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>-->
            <!-- <el-form-item label="订单编号" prop="projectId">
                <el-input v-model="queryParams.projectId" placeholder="请输入订单编号"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item> -->
            <!--<el-form-item label="订单名称" prop="projectName">
                <el-input v-model="queryParams.projectName" placeholder="请输入订单名称"
                        clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>-->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
<!--
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                        v-hasPermi="['develop:GoodsSaleMaterial:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                        @click="handleUpdate" v-hasPermi="['develop:GoodsSaleMaterial:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-popconfirm title="确认删除选择的数据项？" @confirm="handleDelete({})">
                    <el-button slot="reference" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                               v-hasPermi="['develop:GoodsSaleMaterial:remove']">删除</el-button>
                </el-popconfirm>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini"
                        @click="handleExport" v-hasPermi="['develop:GoodsSaleMaterial:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table" :max-height="tableMaxHeight" show-summary :summary-method="getSummaries">
            <el-table-column type="selection" width="55" align="center"/>produceNo
            <el-table-column label="生产单号" align="center" prop="produceNo"/>
            <el-table-column label="订单编号" align="center" prop="projectNo"/>
            <el-table-column label="生产产品编码" align="center" prop="scaleCode"/>
            <el-table-column label="生产产品名称" align="center" prop="scaleName"/>
            <el-table-column label="生产产品型号" align="center" prop="xinghao"/>
            <el-table-column label="物料编码" align="center" prop="goodsCode"/>
            <el-table-column label="物料名称" align="center" prop="goodsName"/>
           <!-- <el-table-column label="物料型号明细" align="center" prop="goodsBrand"/>-->
            <el-table-column label="物料型号" align="center" prop="goodModel"/>
            <el-table-column label="需物料数量" align="center" prop="goodsCount"/>
            <el-table-column label="领取数量" align="center" prop="lingquCount"/>
            <el-table-column label="剩余数量" align="center" prop="shengyuCount"/>
            <<!--el-table-column label="物料单价" align="center" prop="goodsBuyPrice"/>
            <el-table-column label="物料本价" align="center" prop="goodsBuyTotal"/>-->
            <!--<el-table-column label="供应商" align="center" prop="supplierId"/>
            <el-table-column label="供应商名称" align="center" prop="supplierName"/>-->
         <!--   <el-table-column label="销售人姓名" align="center" prop="saleName"/>-->
            <el-table-column label="备注" align="center" prop="remark"/>
          <!--<el-table-column label="订单编号" align="center" prop="projectId"/>
          <el-table-column label="订单名称" align="center" prop="projectName"/>-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-edit"
                             @click="lingliaoMingxi(scope.row)" v-hasPermi="['develop:GoodsSaleMaterial:detailBnt']">领料明细</el-button>
                  <el-button size="mini" type="text" icon="el-icon-edit"
                             @click="lingliao(scope.row)" v-hasPermi="['develop:GoodsSaleMaterial:getBnt']">领 料</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit"
                            @click="handleUpdate(scope.row)" v-hasPermi="['develop:GoodsSaleMaterial:lookDetailBnt']">查看详情</el-button>
                    <!--<el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                                   style="margin-left: 10px;">
                        <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                                   v-hasPermi="['develop:GoodsSaleMaterial:removeBnt']">删除</el-button>
                    </el-popconfirm>-->
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

        <goodsSaleMaterial-form ref="form" @ok="getList"></goodsSaleMaterial-form>
        <lingliaoMingxi-form-list ref="lingliaoMingxiForm" @ok="getList"></lingliaoMingxi-form-list>
        <rukuForm-list ref="rukuForm" @ok="getList"></rukuForm-list>
    </div>
</template>

<script>
    import { deleteAction, getAction,accAdd } from '@/api/manage'
    import GoodsSaleMaterialForm from '@/views/develop/goodsSaleMaterial/module/GoodsSaleMaterialForm'
    import RukuFormList from '@/views/develop/goodsSaleMaterial/module/RukuFormList'
    import LingliaoMingxiFormList from '@/views/develop/goodsSaleMaterial/module/LingliaoMingxiFormList'

    export default {
        name: "GoodsSaleMaterialList",
        components: {
            GoodsSaleMaterialForm,
            RukuFormList,
            LingliaoMingxiFormList
        },
        props: {
            projectCode: {
                type: [String],
                required: true,
                default: ''
            },
        },
        data() {
            return {
                title:'',
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 销售产品物料表格数据
                dataList: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    gsaleId: null,
                    goodsId: null,
                    goodsCode: null,
                    goodsName: null,
                    goodsType: null,
                    goodsBrand: null,
                    goodModel: null,
                    goodsBuyPrice: null,
                    goodsBuyTotal: null,
                    supplierId: null,
                    supplierName: null,
                    saleName: null,
                    projectId: null,
                    projectName: null,
                    projectNo: null,
                    status: null,
                    produceNo: null,
                    orderByColumn: "shengyuCount",
                    isAsc: "desc",
                },
                url: {
                    list:'/goodsSaleMaterial/goodsSaleMaterial/list',
                    remove:'/goodsSaleMaterial/goodsSaleMaterial/',
                    export:'/goodsSaleMaterial/goodsSaleMaterial/export',
                },
                tableMaxHeight: window.innerHeight - 320,
            };
        },
        watch: {
            'projectCode': {
                handler(newVal, oldVal) {
                  console.log("projectCode", newVal);
                    if (newVal) {
                        this.queryParams.projectNo = newVal;
                        this.getList();
                    }
                },
                deep: true,
                immediate: true
            },
        },
        created() {
            this.getList();
            window.addEventListener('resize', this.getTableHeight);
        },
        destroyed () {
            window.removeEventListener('resize', this.getTableHeight)
        },
        methods: {
            getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if ( column.property === "goodsCount" || column.property === "shengyuCount" || column.property === "lingquCount") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev , curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
            lingliaoMingxi(row){
              this.$refs.lingliaoMingxiForm.show(row);
            },
            lingliao(row){
              this.$refs.rukuForm.showRuku(row);
            },
            /** 查询销售产品物料列表 */
            getList() {
                this.loading = true;
                getAction(this.url.list, this.queryParams).then(response => {
                    this.dataList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            getTableHeight () {
                this.tableHeight = window.innerHeight - 320
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.$refs.form.handleAdd();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.$refs.form.handleUpdate(row);
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                deleteAction(this.url.remove, ids).then(response => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {});
            },
            /** 导出按钮操作 */
            handleExport() {
                this.download(this.url.export, {
                    ...this.queryParams
                }, `goodsSaleMaterial_${new Date().getTime()}.xlsx`)
            },
  }
};
</script>
